package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.recruitment_management_system.pojo.entity.RecruitProcess;
import com.team.recruitment_management_system.pojo.vo.RecruitProcessAndJobVo;
import com.team.recruitment_management_system.pojo.vo.RecruitProcessVo;
import com.team.recruitment_management_system.service.RecruitProcessService;
import com.team.recruitment_management_system.mapper.RecruitProcessMapper;
import com.team.recruitment_management_system.util.JwtHelper;
import com.team.recruitment_management_system.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86147
* @description 针对表【recruit_process(招聘流程表)】的数据库操作Service实现
* @createDate 2024-03-21 20:28:40
*/
@Service
public class RecruitProcessServiceImpl extends ServiceImpl<RecruitProcessMapper, RecruitProcess>
    implements RecruitProcessService {

    @Autowired
    RecruitProcessMapper recruitProcessMapper;

    @Autowired
    private JwtHelper jwtHelper;


    @Override
    public Result insert(RecruitProcess recruitProcess) {
        LambdaQueryWrapper<RecruitProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RecruitProcess::getResumeId,recruitProcess.getResumeId())
                .eq(RecruitProcess::getJobId,recruitProcess.getJobId());
        if(this.exists(queryWrapper)){
            return Result.build(null,500,"该职位已投递过");
        }
        return Result.ok(recruitProcessMapper.insert(recruitProcess));

    }

    @Override
    public List<RecruitProcess> getByresumeId(int id) {
        QueryWrapper<RecruitProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resume_id", id);
        List<RecruitProcess> recruitProcesses = recruitProcessMapper.selectList(queryWrapper);
        return recruitProcesses;
    }

    @Override
    public List<RecruitProcess> getByjobId(int id) {
        QueryWrapper<RecruitProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_id", id);
        List<RecruitProcess> recruitProcesses = recruitProcessMapper.selectList(queryWrapper);
        return recruitProcesses;
    }

    @Override
    public List<RecruitProcess> getBystatus(int id) {
        QueryWrapper<RecruitProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", id);
        List<RecruitProcess> recruitProcesses = recruitProcessMapper.selectList(queryWrapper);
        return recruitProcesses;
    }

    @Override
    public List<Integer> getResume(int hrId) {
        //通过招聘流程级联hr再级联job
        List<RecruitProcessVo> list = recruitProcessMapper.hrGetResumeById(hrId);
        List<Integer> resumeIds = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            RecruitProcessVo recruitProcessVo = list.get(i);
            resumeIds.add(recruitProcessVo.getResumeId());
        }

        return resumeIds;
    }

    @Override
    public List<RecruitProcessVo> getRecruitProcess(int hrId) {
        return recruitProcessMapper.hrGetResumeById(hrId);
    }

    @Override
    public List<RecruitProcessAndJobVo> getMineRecruitProcess(String authorization) {
        Integer userId = jwtHelper.getUserId(authorization);
        return recruitProcessMapper.getMineRecruitProcess(userId);
    }
}



